<template>
  <van-goods-action route>
    <van-goods-action-icon icon="chat-o" text="客服"  />
    <van-goods-action-icon icon="cart-o" text="购物车" :badge="badge" @click="toshopCar" />
    <van-goods-action-button type="warning" text="加入购物车" @click="ShowSku"  />
    <van-goods-action-button type="danger" text="立即购买" @click="ShowSku" />
  </van-goods-action>
</template>

<script>
export default {
  data() {
    return {
      badge:0,
      goodsList:JSON.parse(localStorage.getItem("goodsList")) || []
      // JSON.parse(localStorage.getItem("goodsList")) || 
    };
  },
  methods: {
    toshopCar() {
      this.$router.push({ path: "/shopCar" });
    },
    // 点击显示sku弹窗
    ShowSku(){
      this.$eventBus.$emit("ShowSku",true)
    },
    /*getShopCarList() {
      this.goodsList = JSON.parse(localStorage.getItem("goodsList"));
      console.log(this.goodsList)
    },*/
    getAllCount(list){
      let count = 0
      list.forEach((item)=>{
        count += item.num
      })
      return count
    }
  },
  watch:{
    goodsList:{
      handler(newVal){
        this.badge = this.getAllCount(newVal)
      },
      immediate:true
    }
  },
  created(){
    //this.getShopCarList()
    this.$eventBus.$on("getgoodsList",(res)=>{
      this.badge = this.getAllCount(res)
    })
  }
};
</script>

<style>
</style>